/**
 * 
 */
package com.yidu.drug.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.drug.common.Page;
import com.yidu.drug.model.DrugManagerModel;
import com.yidu.drug.model.InventoryModel;
import com.yidu.drug.model.WholesaleDetailModel;
import com.yidu.drug.service.WholesaleDetaiService;

/**
 * 类的描述 批发订单明细action层
 * @author yuanqing
 * @date 2018年8月21日上午9:08:10
 * @version 1.0
 */
@Controller
@RequestMapping("/wholesaleDetail")
public class WholesaleDetailAction {
	@Resource
	private WholesaleDetaiService wholesaleService;
	
	
	/**
	 * 查询所有和条件查询分页(批发退货审核的查看详情)
	 * @param response 响应
	 * @param me 会员对象
	 * @param page 页数
	 * @param limit 行数
	 * @return
	 */
	@RequestMapping("wholesaleFindAll")
	@ResponseBody
	public Map<String, Object> wholesaleFindAll(HttpServletResponse response,WholesaleDetailModel wd,int page,int limit) {
		
		response.setCharacterEncoding("UTF-8");
		Page pages = new Page();
		pages.setPage(page);
		pages.setRows(limit);
		Map<String,String> map=new HashMap<>(); 
		map.put("pfxqlId", wd.getPfxqlId());
		map.put("pfId", wd.getPfId());
		map.put("start", String.valueOf(pages.getStart()));
		map.put("rows", String.valueOf(pages.getRows()));
		List<WholesaleDetailModel> list = wholesaleService.findAllPage(map);
		Map<String, Object> maps = new HashMap<>();
		//需要符合layUI格式，code msg count data
		maps.put("code", 0);
		maps.put("msg", "1");
		maps.put("count", wholesaleService.findTotalRows(wd));
		maps.put("data", list);
		wd=new WholesaleDetailModel();
		
		return maps;
	}
	
	/**
	 * 批发的查看详情
	 * @param session
	 * @param sellId
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/findByPfIds")
	@ResponseBody
	public Map<String, Object> findByPfIds(HttpSession session,String pfId,int page,int limit){
		System.out.println(pfId+"=====================================================");
		WholesaleDetailModel wholesaleDetailModel =new WholesaleDetailModel();
		wholesaleDetailModel.setPfId(pfId);
		Page pages = new Page();
		pages.setPage(page);
		pages.setRows(limit);
		List<WholesaleDetailModel> list=wholesaleService.findByPfIds(wholesaleDetailModel, page, limit);
		Map<String, Object> map=new HashMap<String , Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",wholesaleService.findRows(pfId));
		map.put("data", list);
		return map;
	}
	
	
	
	
	
	
	
	
	
	
	/**
	 * 根据批发订单的Id查询批发详情
	 * @param response 响应
	 * @param pfId 批发Id
	 * @return 批发详情集合
	 */
	@RequestMapping("wholesaleDetailFindAll")
	@ResponseBody
	public List<WholesaleDetailModel>  wholesaleDetailFindAll(HttpServletResponse response,String pfId){
		System.out.println("==============="+"进来了"+pfId);
		response.setCharacterEncoding("UTF-8");
		
		List<WholesaleDetailModel> list = wholesaleService.findPfId(pfId);
		System.out.println(list.size()+"------------");
		
		return list;
		
	}
	/**
	 * 根据药品Id查询药品信息
	 * @param inven 库存对象
	 * @return 药品信息集合s
	 */
	@RequestMapping("storage")
	@ResponseBody
	public WholesaleDetailModel storage(InventoryModel inven){
		
		WholesaleDetailModel whole=wholesaleService.findDrugId(inven.getPwId(),inven.getNumbers());
		if(whole!=null){
			whole.setPfxqNumber(inven.getInvNumber());
			
			whole.setPfxqSubtotal(inven.getInvNumber()*whole.getPfxqPrice());
		}
		
		return whole;
	}
	
	/**
	 * 新增采购订单
	 * @param session 取到登录用户信息
	 * @param wholesaleDetail 批发订单详情对象
	 * @param arr 页面批发信息
	 * @param sum 药品总数量
	 * @param totals 药品总金额
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session,WholesaleDetailModel wholesaleDetail,String arr,String sum,String totals) {
		DrugManagerModel manager=(DrugManagerModel) session.getAttribute("manager");
		
		String msg=wholesaleService.insertAll(manager, wholesaleDetail, arr, sum, totals);
		return msg;
	}
	/**
	 * 新增采购订单
	 * @param session 取到登录用户信息
	 * @param wholesaleDetail 批发订单详情对象
	 * @param arr 页面批发信息
	 * @param sum 药品总数量
	 * @param totals 药品总金额
	 * @return
	 */
	@RequestMapping("/updates")
	@ResponseBody
	public String updates(HttpSession session,WholesaleDetailModel wholesaleDetail,String arr,String sum,String totals,String pfId) {
		DrugManagerModel manager=(DrugManagerModel) session.getAttribute("manager");
		
		String msg=wholesaleService.updateAll(manager, wholesaleDetail, arr, sum, totals,pfId);
		return msg;
	}
	
	
}
